package com.platform.modules.sys.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.utils.Query;
import com.platform.modules.sys.dao.SysUserSelectCourseDao;
import com.platform.modules.sys.entity.SysJmCourseEntity;
import com.platform.modules.sys.entity.SysJmCourseUserEntity;
import com.platform.modules.sys.entity.SysUserEntity;
import com.platform.modules.sys.service.SysUserSelectCourseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author zdz
 * @Title:
 * @Package
 * @Description:
 * @date 2021/7/3 1:45
 */
@Service("selectCoureService")
public class SysUserSelectCourseServiceImpl extends ServiceImpl<SysUserSelectCourseDao, SysJmCourseEntity> implements SysUserSelectCourseService {


    @Override
    public Page queryMustSelectPage(Map<String, Object> params) {
        Page<SysJmCourseEntity> page = new Query<SysJmCourseEntity>(params).getPage();
        return page.setRecords(baseMapper.selectMuestSelectSysJmCoursePage(page, params));
    }

    @Override
    public Page queryMaySelectPage(Map<String, Object> params) {
        Page<SysJmCourseEntity> page = new Query<SysJmCourseEntity>(params).getPage();
        return page.setRecords(baseMapper.selectMaySelectSysJmCoursePage(page, params));
    }

    @Override
    public List<String> queryAllMayCourseIds(Map<String, Object> params) {
        return baseMapper.queryAllMayCourseIds(params);
    }

    @Override
    public List<SysJmCourseEntity> queryAllMustSelectCourseByModuleId(String moduleId) {
        if (StringUtils.isEmpty(moduleId)) {
            return Collections.emptyList();
        }
        return baseMapper.queryAllMustSelectCourseByModuleId(moduleId);
    }

    @Override
    public List<SysJmCourseEntity> queryMaySelectCourseByIds(String[] courseIds) {
        if (null == courseIds || courseIds.length == 0) {
            return Collections.emptyList();
        }
        return baseMapper.queryMaySelectCourseByIds(courseIds);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveUserSelectCourse(SysUserEntity user, String moduleId, String courseIds) {
        if (null == user || StringUtils.isEmpty(moduleId)) {
            return false;
        }
        // 获取当前用户对应专业下的所以必选课
        List<SysJmCourseEntity> mustSelectCoures = baseMapper.queryAllMustSelectCourseByModuleId(moduleId);
        // 获取当前用户对应专业下的所以任选课
        List<SysJmCourseEntity> maySelectCoures = baseMapper.queryMaySelectCourseByIds(courseIds.split(","));
        // 合并两类任务
        List<SysJmCourseEntity> selectCoures = new ArrayList<>();
        selectCoures.addAll(mustSelectCoures);
        selectCoures.addAll(maySelectCoures);
        // 存储用户选课记录
        List<SysJmCourseUserEntity> userSelectCourse = new ArrayList<>();
        // 记录余量足够的任务Id
        List<String> enoughLessCoures = new ArrayList<>();
        // 将当前用户所选的必选课及任选课转成选课记录对象
        buildUserSelectCourse(user, moduleId, selectCoures, userSelectCourse, enoughLessCoures);

        // 修改课程余量
        baseMapper.updateMaySelectCourseLess(user.getUserId(), moduleId);
        // 删除之前已经选择的任务
        baseMapper.deleteAllUserSelect(user.getUserId(), moduleId);

        if (userSelectCourse.isEmpty()) {
            return true;
        }

        if (baseMapper.bathInsert(userSelectCourse) > 0) {
            // 剩余量要扣减
            baseMapper.minusCourseLessById(enoughLessCoures);
            return true;
        }
        return false;
    }


    private void buildUserSelectCourse(SysUserEntity user, String moduleId, List<SysJmCourseEntity> selectCoures, List<SysJmCourseUserEntity> userSelectCourse, List<String> enoughLessCoures) {
        for (SysJmCourseEntity ce : selectCoures) {
            if (Integer.parseInt(ce.getCourseLess()) <= 0) {
                continue;
            }
            enoughLessCoures.add(ce.getId());
            // 构建选课任务对象
            SysJmCourseUserEntity cue = new SysJmCourseUserEntity();
            cue.setCourseId(ce.getId());
            cue.setCourseCode(ce.getCourseCode());
            cue.setCourseName(ce.getCourseName());
            cue.setCourseAddress(ce.getCourseAddress());
            cue.setCourseTime(ce.getCourseTime());
            cue.setCreateTime(new Date());
            cue.setTeacherName(ce.getCourseTeacher());
            cue.setUserId(user.getUserId());
            cue.setUserName(user.getRealName());
            cue.setModuleId(moduleId);
            cue.setCourseDate(ce.getCourseDate());
            userSelectCourse.add(cue);
        }
    }
}
